package com.example.demo.practice.dashboardview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.example.demo.practice.R;
import com.example.demo.practice.httpserver.HttpServer;
import com.example.demo.practice.ui.TitleView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AlarmInfo extends Activity {
    private HttpServer http = new HttpServer();
    private String HTTP = http.getHttp();

    private JSONArray jSONArray;
    private int ID;
    private String eqindex ;
    private String name ;
    private String produdtor ;
    private String buydate ;
    private String buildate ;
    private String status ;
    private String address ;
    private String floor ;
    private String buliduser ;
    private String price ;
    private TitleView title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.facility_info);
        title = findViewById(R.id.title);
        title.setText("报警信息");
        getVideoData();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            for (int i = 0; i < jSONArray.length(); i++) {
                JSONObject json = null;
                try {
                    json = (JSONObject) jSONArray.get(i);
                        eqindex = json.getString("eqindex");
                        name = json.getString("name");
                        produdtor = json.getString("produdtor");
                        String date = json.getString("buydate");
                        String[] AuditDates = date.split("T");
                        buydate = AuditDates[0];
                        date = json.getString("buildate");
                        AuditDates = date.split("T");
                        buildate = AuditDates[0];
                        status = json.getString("status");
                        address = json.getString("address");
                        floor = json.getString("floor");
                        buliduser = json.getString("buliduser");
                        price = json.getString("price");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            jSONArray=null;

            TextView text1 = findViewById(R.id.text1);
            text1.setText("设备编号：" +  eqindex);    //填入相应的值
            TextView text2 = findViewById(R.id.text2);
            text2.setText("设备类型：" + name);
            TextView text3 = findViewById(R.id.text3);
            text3.setText("设备位置：" + address + floor);    //填入相应的值
            TextView text4 = findViewById(R.id.text4);
            text4.setText("报警日期：" + buydate);

            super.handleMessage(msg);
        }
    };

    private void getVideoData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
                    Request request = new Request.Builder()
                            .url(HTTP + "getFacilities")//请求接口。如果需要传参拼接到接口后面。
                            .build();//创建Request 对象

                    Response response = client.newCall(request).execute();//得到Response 对象
                    if (response.isSuccessful()) {
                        Log.e("kwwl", "response.code()==" + response.code());
                        Log.e("kwwl", "response.message()==" + response.message());
                        //response.body().string()只可以调用一次
                        //此时的代码执行在子线程，修改UI的操作请使用handler跳转到UI线程。
                        jSONArray = new JSONArray(response.body().string());
                        handler.sendEmptyMessage(0);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
